package com.huluobo.a2308douyindemo.live

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import com.google.gson.Gson
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.adapter.PayCoinAdapter
import com.huluobo.a2308douyindemo.bean.AccountBean
import com.huluobo.a2308douyindemo.bean.PayCoinBean
import com.huluobo.a2308douyindemo.databinding.ActivityPayBinding
import com.huluobo.a2308douyindemo.mvvm.BaseActivity
import com.huluobo.a2308douyindemo.net.Const
import com.huluobo.a2308douyindemo.net.MessageEvent
import com.huluobo.a2308douyindemo.utils.MySp
import com.huluobo.a2308douyindemo.utils.MyToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PayActivity : BaseActivity<ActivityPayBinding, LiveViewModel>() {
    private lateinit var accountBean: AccountBean
    private lateinit var payCoinAdapter: PayCoinAdapter
    private lateinit var payCoinBean: PayCoinBean
    override fun initData() {
        val coinList = listOf(
            PayCoinBean(6, 60, false),
            PayCoinBean(18, 180, false),
            PayCoinBean(45, 450, false),
            PayCoinBean(128, 1280, false),
            PayCoinBean(328, 3280, false),
            PayCoinBean(648, 6480, false),
        )

        payCoinAdapter.data.addAll(coinList)
        payCoinAdapter.notifyDataSetChanged()

        vm.modifyCoinSuccess.observe(this) {
            //加抖币
            accountBean.coinamount += payCoinBean.coin
            //减余额
            accountBean.accountamount -= payCoinBean.price
            val json = Gson().toJson(accountBean)
            MySp.putString(Const.KEY_ACCOUNT, json)

            binding.payCurrentMoneyTv.text = "当前余额 : ${accountBean.accountamount}"
            binding.payCoinCountTv.text = "当前抖币 : ${accountBean.coinamount}"
        }

        vm.modifyAccountSuccess.observe(this) {

        }
    }

    override fun initView() {
        val accountJson = MySp.get(Const.KEY_ACCOUNT)
        accountBean = Gson().fromJson(accountJson, AccountBean::class.java)

        binding.payCurrentMoneyTv.text = "当前余额 : ${accountBean.accountamount}"
        binding.payCoinCountTv.text = "当前抖币 : ${accountBean.coinamount}"

        binding.payPriceRv.layoutManager = GridLayoutManager(this, 3)
        payCoinAdapter = PayCoinAdapter()
        binding.payPriceRv.adapter = payCoinAdapter

        payCoinAdapter.setOnItemClickListener { adapter, view, position ->
            //单选效果
            payCoinAdapter.data.forEachIndexed { index, payCoinBean ->
                payCoinBean.isSelect = position == index
            }

            //选中的金额
            payCoinBean = payCoinAdapter.data[position]
            adapter.notifyDataSetChanged()
        }

        binding.payCoinBtn.setOnClickListener {
            //判断一个延迟初始化的对象是否已经被初始化过
            //this 当前类中的payCoinBean属性是否被初始化过
            if (this::payCoinBean.isInitialized) {

                if (accountBean.accountamount > payCoinBean.price) {
                    //++抖币
                    vm.modifyCoin(accountBean.id, payCoinBean.coin.toString())

                    //--余额
                    vm.modifyAccount(accountBean.id, (-payCoinBean.price).toString())
                } else {
                    MyToast.showLong("余额不足,请充值")
                }
            } else {
                MyToast.showLong("初始化还未完成,请稍等...")
            }
        }

        binding.payAccountBtn.setOnClickListener {
            startAct(PayAccountActivity::class.java)
        }
    }

    //2种方式可以获取到最新的账户信息
    //第一种,通过生命周期,当页面恢复的时候,重新通过sp获取账户信息
//    override fun onResume() {
//        super.onResume()
//        val accountJson = MySp.get(Const.KEY_ACCOUNT)
//        accountBean = Gson().fromJson(accountJson, AccountBean::class.java)
//        binding.payCurrentMoneyTv.text = "当前余额 : ${accountBean.accountamount}"
//    }

    //第二种方式,通过EventBus,去更新信息
    override fun onEvent(messageEvent: MessageEvent) {
        super.onEvent(messageEvent)
        val money = messageEvent.message
        binding.payCurrentMoneyTv.text = "当前余额 : $money"
    }
}